package com.xca.znb.view;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.xca.znb.R;
import com.xca.znb.adapter.SchoolFragmentAdapter;
import com.xca.znb.contact.SaleHomeContact;
import com.xca.znb.entity.News;
import com.xca.znb.model.impl.SaleHomeImpl;
import com.xca.znb.presenter.SaleHomePresenter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class SaleHomeFragment extends Fragment implements View.OnClickListener , SaleHomeContact {
    private SaleHomePresenter saleHomePresenter ;
    private ImageButton GoBuy ;
    private View sale_home ;
    private LinearLayout MyGoods , MyProcurement , MyOrder , MyStore ;
    private TextView TodaySales , MonthSales , TotalSales , newsMore;
    private ListView listView ;
    private List<News> newsList ;
    private SchoolFragmentAdapter adapter ;
    private List<News> list3 ;
    public SaleHomeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       if (sale_home == null){
           sale_home = inflater.inflate(R.layout.my_store , container , false);
       }

       SaleHomeImpl saleHomeImpl = new SaleHomeImpl(getActivity());
       saleHomePresenter = new SaleHomePresenter(this , saleHomeImpl);
       GoBuy = sale_home.findViewById(R.id.go_buy);
       MyGoods = sale_home.findViewById(R.id.my_goods);
       MyProcurement = sale_home.findViewById(R.id.procurement_management) ;
       MyOrder = sale_home.findViewById(R.id.orders_management) ;
       MyStore = sale_home.findViewById(R.id.store_management) ;
       TodaySales = sale_home.findViewById(R.id.today_sales_number) ;
       MonthSales = sale_home.findViewById(R.id.month_sales_number) ;
       TotalSales = sale_home.findViewById(R.id.total_sales_number) ;
       listView = sale_home.findViewById(R.id.my_store_listView);
       newsMore = sale_home.findViewById(R.id.my_store_more_btn);
       onGoBuyButtonClick();

       MyGoods.setOnClickListener(this);
       MyOrder.setOnClickListener(this);
       MyProcurement.setOnClickListener(this);
       MyStore.setOnClickListener(this);
       MyOrder.setOnClickListener(this);


        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
        String phone = sharedPreferences.getString("phone" , "");

        LocalDateTime nowTime = LocalDateTime.now();
        String dayTime = nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String monthTime = nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM")) ;

        double dSales = saleHomePresenter.getDaySales(phone,dayTime);
        double mSales = saleHomePresenter.getMonthSales(phone,monthTime);
        double tSales = saleHomePresenter.getTotalSales(phone);

        TodaySales.setText(String.valueOf(dSales));
        MonthSales.setText(String.valueOf(mSales));
        TotalSales.setText(String.valueOf(tSales));


        newsList = saleHomePresenter.getNews();
        if (newsList != null){
            adapter = new SchoolFragmentAdapter(getActivity() , newsList);
            listView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity() , Main.class);
                intent.putExtra("toSchoolFragment" , 100);
                startActivity(intent);
                getActivity().finish();
            }
        });
        newsMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity() , Main.class);
                intent.putExtra("toSchoolFragment" , 100);
                startActivity(intent);
                getActivity().finish();
            }
        });
       return sale_home ;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.go_buy){
            Intent intent = new Intent(getActivity() , Main.class);
            startActivity(intent);
            getActivity().finish();
        } else if (v.getId() == R.id.my_goods) {
            Intent intent = new Intent(getActivity() , MyGoodsActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.procurement_management) {
            Intent intent = new Intent(getActivity() , MyProcurementActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.orders_management) {
            Intent intent = new Intent(getActivity() , OrdersActivity.class) ;
            intent.putExtra("case" , 6) ;
            startActivity(intent);
        } else if (v.getId() == R.id.store_management) {
            Intent intent = new Intent(getActivity() , MyStoreManagerActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public void onGoBuyButtonClick() {
        GoBuy.setOnClickListener(this);
    }
}